use rusty_leveldb::{Options, DB};

fn main() {
    let mut db = DB::open("db_files/test_write_a_lot", Options::default()).unwrap();

    let rs = db.put("hello".as_bytes(), "world".as_bytes());
    println!("put result: {:?}", rs);

    let rs = db.get("hello".as_bytes());

    println!("get result: {:?}", String::from_utf8(rs.unwrap().to_vec().into()));

    let mut db = insert_too_many_data(db);

    let rs = db.get("hello".as_bytes());
    println!("get result: {}", String::from_utf8(rs.unwrap().to_vec().into()).unwrap());
}

fn insert_too_many_data(mut db: DB) -> DB {
    for i in 0..3_000_000 {
        let _ = db.put(i.to_string().as_bytes(), format!("val{}", i).as_bytes());
        let get_rs = db.get((i - 1).to_string().as_bytes());
        match get_rs {
            Some(val) => {
                let rs = String::from_utf8(val.to_vec()).unwrap();
                let expect = format!("val{}", i - 1);
                assert_eq!(rs, expect, "expect {} get result: {}", expect, rs);
            }
            None => {
                println!("key {} get result: None", i - 1);
            }
        }
    }
    db
}
